package com.tiancheng.trade.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.BusinessException;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.order.enums.UmsTradeTypeEnum;
import com.tiancheng.trade.order.service.MessageSubscribeService;
import com.tiancheng.trade.order.util.cmq.MessageParsing;
import com.tiancheng.trade.order.util.ums.UmsAcpService;
import com.tiancheng.trade.order.util.ums.UmsBindingBankCardConfig;
import com.tiancheng.trade.order.util.ums.UmsConstant;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: ld
 * @Date: 2024/3/16 15:04
 * @Param ${tags}
 * @Description: 订单中心接受异步回调方法
 */
@RestController
@RequestMapping("/notify")
@Slf4j
public class OrderNotifyController {

	@Resource
	private MessageSubscribeService messageSubscribeService;
	@Resource
	private MessageParsing messageParsing;
	@Resource
	private UmsBindingBankCardConfig umsConfig;

	/**
	 * 接收消息
	 * @param message
	 * @return
	 */
	@PostMapping(value = "/message/subscribe")
	public Result messageSubscribe(@RequestBody String message,HttpServletResponse response) {
		try {
			log.info("接收cmq消息通知:{}", message);
			messageSubscribeService.handle(message);
		} catch (BusinessException be){
			log.error("接收cmq消息通知异常，", be);
			log.error(be.getMessage(), be);
			if(be.getErrorInfo().getCode()==BusinessErrorInfoEnum.ORDER_PAY_NOTIFY_ERROR.getCode()){
				response.setStatus(500);
				try {
					response.getWriter().write(JsonUtil.toJsonString(Result.fail("处理异常")));
				} catch (IOException e) {
					log.error("接收cmq消息通知异常，", e);
				}
				return Result.fail("处理异常");
			}
		}catch (Exception e) {
			log.error("接收cmq消息通知异常，", e);
			log.error(e.getMessage(), e);
		}
		return Result.success();
	}

	/**
	 * 接收消息
	 * @param req
	 * @return
	 */
	@PostMapping(value = "/bindingBankCard")
	public Map<String, String> bindingBankCardMessageSubscribe(HttpServletRequest req) {
		String code="00";
		try {
			// 获取银联通知服务器发送的后台通知参数
			Map<String, String> reqParam = UmsAcpService.getAllRequestParam(req);
			log.info("接收银联绑卡回调信息: param:{}", JSON.toJSONString(reqParam));
			messageSubscribeService.bindingBankCardCallBack(reqParam);
		} catch (BusinessException e) {
			log.error("银联绑卡回调处理异常:{}",e.getMessage(), e);
			if(e.getErrorInfo().getCode()!= BusinessErrorInfoEnum.BANK_CARD_BINDING_CALLBACK_REPEAT.getCode()){
				// 非重复回调返回银联失败
				code="36";
			}
		} catch (Exception e) {
			log.error("银联绑卡回调处理异常:{}",e.getMessage(), e);
			code="36";
		}

		Map<String, String> respMap = new HashMap<String, String>();
		respMap.put("reqType", UmsTradeTypeEnum.BINDING_NOTICE.getCode());
		respMap.put("version", UmsConstant.VERSION);
		respMap.put("respCode", code);
		respMap.put("respMsg", code.equals("00")?"成功":"未知异常");
		return UmsAcpService.sign(respMap, umsConfig);
	}

	/**
	 * 接收消息
	 * @param params
	 * @return
	 */
	@PostMapping(value = "/bindingBankCardJson")
	public Map<String, String> bindingBankCardMessageSubscribeJson(@RequestBody String params) {
		String code="00";
		try {
			// 获取银联通知服务器发送的后台通知参数
			Map<String, String> reqParam = JSON.parseObject(params, new TypeReference<Map<String, String>>() {
			});
			log.info("接收银联绑卡回调信息: param:{}", JSON.toJSONString(reqParam));
			messageSubscribeService.bindingBankCardCallBack(reqParam);
		} catch (BusinessException e) {
			log.error("银联绑卡回调处理异常:{}",e.getMessage(), e);
			if(e.getErrorInfo().getCode()!= BusinessErrorInfoEnum.BANK_CARD_BINDING_CALLBACK_REPEAT.getCode()){
				// 非重复回调返回银联失败
				code="36";
			}
		} catch (Exception e) {
			log.error("银联绑卡回调处理异常:{}",e.getMessage(), e);
			code="36";
		}

		Map<String, String> respMap = new HashMap<String, String>();
		respMap.put("reqType", UmsTradeTypeEnum.BINDING_NOTICE.getCode());
		respMap.put("version", UmsConstant.VERSION);
		respMap.put("respCode", code);
		respMap.put("respMsg", code.equals("00")?"成功":"未知异常");
		return UmsAcpService.sign(respMap, umsConfig);
	}

}
